{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "20326118",
   "metadata": {},
   "source": [
    "# Install"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2422f32f",
   "metadata": {},
   "source": [
    "## Install BenchMARL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b10fd32",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title\n",
    "!git clone https://github.com/facebookresearch/BenchMARL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cd7b69b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title\n",
    "%cd /content/BenchMARL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f32b88e",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title\n",
    "!pip install -U torch torchvision\n",
    "!pip install -e ."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "585d3a35",
   "metadata": {},
   "source": [
    "## Install VMAS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2e551b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title\n",
    "!pip install vmas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33d72783",
   "metadata": {},
   "outputs": [],
   "source": [
    "#@title\n",
    "!apt-get update\n",
    "!apt-get install -y x11-utils \n",
    "!apt-get install -y xvfb\n",
    "!apt-get install -y imagemagick\n",
    "!pip install pyvirtualdisplay\n",
    "import pyvirtualdisplay\n",
    "display = pyvirtualdisplay.Display(visible=False, size=(1400, 900))\n",
    "display.start()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "caa7225f",
   "metadata": {},
   "source": [
    "# Launch from command line"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30075032",
   "metadata": {},
   "source": [
    "To launch an experiment from the command line you can do"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5369898f",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "!python benchmarl/run.py algorithm=mappo task=vmas/balance experiment.max_n_iters=2 \"experiment.loggers=[]\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23f9338f",
   "metadata": {},
   "source": [
    "You can run benchmarks as multi-runs like"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90a135ea",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "!python benchmarl/run.py -m algorithm=mappo,qmix,masac task=vmas/balance,vmas/sampling seed=0,1 experiment.max_n_iters=2 \"experiment.loggers=[]\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b01091a",
   "metadata": {},
   "source": [
    "# Launch from a python script"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67c6dc69",
   "metadata": {},
   "source": [
    "You can also load and launch your experiments from within a script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c5b5fcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "from benchmarl.algorithms import MappoConfig\n",
    "from benchmarl.environments import VmasTask\n",
    "from benchmarl.experiment import Experiment, ExperimentConfig\n",
    "from benchmarl.models.mlp import MlpConfig\n",
    "\n",
    "# Loads from \"benchmarl/conf/experiment/base_experiment.yaml\"\n",
    "experiment_config = ExperimentConfig.get_from_yaml()\n",
    "# Loads from \"benchmarl/conf/task/vmas/balance.yaml\"\n",
    "task = VmasTask.BALANCE.get_from_yaml()\n",
    "# Loads from \"benchmarl/conf/algorithm/mappo.yaml\"\n",
    "algorithm_config = MappoConfig.get_from_yaml()\n",
    "# Loads from \"benchmarl/conf/model/layers/mlp.yaml\"\n",
    "model_config = MlpConfig.get_from_yaml()\n",
    "critic_model_config = MlpConfig.get_from_yaml()\n",
    "\n",
    "experiment_config.max_n_iters = 2\n",
    "experiment_config.loggers = []\n",
    "\n",
    "experiment = Experiment(\n",
    "    task=task,\n",
    "    algorithm_config=algorithm_config,\n",
    "    model_config=model_config,\n",
    "    critic_model_config=critic_model_config,\n",
    "    seed=0,\n",
    "    config=experiment_config,\n",
    ")\n",
    "experiment.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a20864e3",
   "metadata": {},
   "source": [
    "You can also run multiple experiments in a Benchmark."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ec3bd4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from benchmarl.algorithms import MappoConfig, MasacConfig, QmixConfig\n",
    "from benchmarl.benchmark import Benchmark\n",
    "from benchmarl.environments import VmasTask\n",
    "from benchmarl.experiment import ExperimentConfig\n",
    "from benchmarl.models.mlp import MlpConfig\n",
    "\n",
    "# Loads from \"benchmarl/conf/experiment/base_experiment.yaml\"\n",
    "experiment_config = ExperimentConfig.get_from_yaml()\n",
    "# Loads from \"benchmarl/conf/task/vmas\"\n",
    "tasks = [VmasTask.BALANCE.get_from_yaml(), VmasTask.SAMPLING.get_from_yaml()]\n",
    "# Loads from \"benchmarl/conf/algorithm\"\n",
    "algorithm_configs = [\n",
    "    MappoConfig.get_from_yaml(),\n",
    "    QmixConfig.get_from_yaml(),\n",
    "    MasacConfig.get_from_yaml(),\n",
    "]\n",
    "# Loads from \"benchmarl/conf/model/layers\"\n",
    "model_config = MlpConfig.get_from_yaml()\n",
    "critic_model_config = MlpConfig.get_from_yaml()\n",
    "\n",
    "experiment_config.max_n_iters = 2\n",
    "experiment_config.loggers = []\n",
    "\n",
    "benchmark = Benchmark(\n",
    "    algorithm_configs=algorithm_configs,\n",
    "    tasks=tasks,\n",
    "    seeds={0, 1},\n",
    "    experiment_config=experiment_config,\n",
    "    model_config=model_config,\n",
    "    critic_model_config=critic_model_config,\n",
    ")\n",
    "benchmark.run_sequential()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
